package com.wzq.app.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.LoopPagerAdapter;
import com.wzq.app.R;
import com.wzq.app.api.Api;
import com.wzq.app.api.ApiResponseCode;
import com.wzq.app.base.BaseFragment;
import com.wzq.app.constant.App;
import com.wzq.app.manager.UserManager;
import com.wzq.app.model.BaseModel;
import com.wzq.app.model.StudyModel;
import com.wzq.app.ui.VideoActivity;
import com.wzq.app.ui.recyclerview.CommonAdapter;
import com.wzq.app.ui.recyclerview.base.ViewHolder;
import com.wzq.app.utils.GlideUtils;
import com.wzq.app.utils.ToastUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @ProjectName: wzq
 * @Package: com.wzq.app.ui.fragment
 * @ClassName: FirstFragment
 * @Description: java类作用描述
 * @Author: Tao.xiaolong
 * @CreateDate: 2021/2/23 13:25
 * @UpdateUser: 更新者：
 * @UpdateDate: 2021/2/23 13:25
 * @UpdateRemark: 更新说明：
 * @Version: banner1.0
 */
public class FirstFragment extends BaseFragment {
    private static final String TAG = "FirstFragment";
    @BindView(R.id.viewpager)
    RollPagerView viewpager;
    Unbinder unbinder;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    private TestLoopAdapter mAdapter;
    private SiteListAdapter mSiteAdapter;

    int[] imgs ={R.drawable.banner4,R.drawable.banner5};
    @NonNull
    @Override
    public int getLayoutId() {
        return R.layout.main_fragment;
    }

    @Override
    public void configView() {
        mAdapter = new TestLoopAdapter(viewpager);
        viewpager.setAdapter(mAdapter);
        mSiteAdapter = new SiteListAdapter(mContext);
        recyclerView.setAdapter(mSiteAdapter);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setNestedScrollingEnabled(false);
    }

    @Override
    public void requestData() {
        Map<String, Integer> map = new HashMap<>();
        if (!App.TEACHER.equals(UserManager.getInstance().getUser().getType())) {
            map.put("unit", Integer.valueOf(UserManager.getInstance().getUser().getUnit()));
        }
        map.put("offset", 0);
        map.put("page", 1);
        map.put("limit", 10);
        Call<BaseModel<StudyModel>> call = Api.getInstance().service.getStudyList(map);
        call.enqueue(new Callback<BaseModel<StudyModel>>() {
            @Override
            public void onResponse(Call<BaseModel<StudyModel>> call, Response<BaseModel<StudyModel>> response) {
                if (response.isSuccessful()) {
                    BaseModel<StudyModel> model = response.body();
                    if (model.code == ApiResponseCode.RESPHONE_OK) {
                        Log.d(TAG, response.toString());
                        StudyModel studyModel = model.obj;
                        int t = studyModel.getTotal();
                        List<StudyModel.RowsBean> list = studyModel.getRows();
                        mSiteAdapter.refreshView(list);
                    } else {
                        mSiteAdapter.clear();
                        mSiteAdapter.refreshView(model.obj.getRows());
                        ToastUtils.showToast(mContext, "暂无教学资源，请联系老师咨询");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseModel<StudyModel>> call, Throwable t) {
                ToastUtils.showToast(mContext, "查询资源失败，请检查网络");
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }


    public class SiteListAdapter extends CommonAdapter<StudyModel.RowsBean> {

        public SiteListAdapter(Context context) {
            super(context);
        }

        @Override
        protected void convert(ViewHolder holder, final StudyModel.RowsBean rowsBean, int position) {
            holder.setText(R.id.study_name, rowsBean.getResourceName());
            switch (Integer.valueOf(rowsBean.getUnit())) {
                case 1:
                    holder.setText(R.id.unit, "一年级");
                    break;
                case 2:
                    holder.setText(R.id.unit, "二年级");
                    break;
                case 3:
                    holder.setText(R.id.unit, "三年级");
                    break;
                case 4:
                    holder.setText(R.id.unit, "四年级");
                    break;
                case 5:
                    holder.setText(R.id.unit, "五年级");
                    break;
                case 6:
                    holder.setText(R.id.unit, "六年级");
                    break;
                default:
                    break;
            }
            holder.setOnClickListener(R.id.layout, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToastUtils.showToast(mContext, rowsBean.getResourceName());
                    Intent intent = new Intent(mContext, VideoActivity.class);
                    intent.putExtra("fileName", rowsBean.getFileName());
                    intent.putExtra("Name", rowsBean.getResourceName());
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getLayoutId() {
            return R.layout.site_real_item;
        }
    }

    private class TestLoopAdapter extends LoopPagerAdapter {

        public TestLoopAdapter(RollPagerView viewPager) {
            super(viewPager);
        }

        @Override
        public View getView(ViewGroup container, final int position) {
            ImageView view = new ImageView(container.getContext());
            GlideUtils.displayImage(mContext, view, imgs[position]);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return view;
        }

        @Override
        public int getRealCount() {
            return 2;
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
